<template>
  <div class="broad_list_detail">
    <div class="broad_list_first">
      <div class="broad_list_first_left">
        <span>广播</span>
      </div>
      <div class="broad_list_first_right">
        <div class="broad_close" @click.stop.prevent="broadClose"></div>
      </div>
    </div>
    <div class="broad_main_w">
      <div class="b_m_t">
        <span>{{ broad_show_item.title }}</span>
      </div>
      <div class="b_m_n">
        <span>{{ broad_show_item.name }}</span>
        <span class="b_m_l"></span>
        <span class="b_m_time">{{ broad_show_item.time | getTimeAll }}</span>
      </div>
      <div class="b_m_v_w">
        <div class="b_m_c" v-html="broad_show_item.value"></div>
        <div class="b_m_f">
          <div class="broad_file_wrap" v-for="(item, index) in broad_show_item.files" :key="'broad_' + index">
            <div class="broad_f_l"></div>
            <div class="broad_f_r">
              <div class="broad_f_name">{{ item.attachName }}</div>
              <div class="broad_f_size">
                <span class="broad_f_size_text">{{item.attachsize | convertByte}}</span>
                <span v-if="item.upload!=1" class="broad_f_dl" @click.stop.prevent="downFile(item)">下载</span>
              </div>
              <Progress class="broad_p" v-if="item.upload==1" :percent="item.progress?item.progress:0" status="active"></Progress>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { mapState } from "vuex";
export default {
  props: {
    pageBroadList: {
      type: Array,
      default: () => {
        return []
      }
    },
    broad_show_item: {
      type: Object
    },
    broad_show_item_index: {
      type: Number
    },
    broadType: {
      type: String,
      default: ""
    }
  },
  data() {
    return {
      isTriggerPageUp: false,
      isTriggerPageDown: false,
      typePage: "",
    };
  },
  watch: {
    pageBroadList() {
      if (this.typePage == "upPage") {
        this.isTriggerPageUp = true;
        this.isTriggerPageDown = false;
      } else if (this.typePage == "downPage") {
        this.isTriggerPageUp = false;
        this.isTriggerPageDown = true;
      }
    },
    broadType() {
      if (this.broadType) {
        // 当前为最后一页最后一条时、当前为第一页第一条时
        if (this.broadType == "noShow") {
          this.isTriggerPageUp = false;
          this.isTriggerPageDown = false;
        }else if (this.broadType == "showUpPage") {
          this.isTriggerPageUp = true;
          this.isTriggerPageDown = false;
        }
        this.typePage = this.broadType;
        this.$emit("searchBroadList", this.broadType);
        this.$store.commit("setBroadType", "");
      }
    },
    typePage() {
      this.pageBroadList();
    }
  },
  computed: {
    ...mapState(["myInfo", "broadType"])
  },
  mounted() {},
  //过滤器filter
  filters: {
    convertByte(byteCount) {
      var count = parseFloat(byteCount || 0),
        unitArr = ["b", "Kb", "Mb", "Gb", "Tb"],
        index = 0;
      if (!count) return 0;
      for (let i = 0; i < 5; i++) {
        if (count >= 1024) {
          count = count / 1024;
          index++;
        } else {
          return index > 0
            ? count.toFixed(2) + unitArr[index]
            : count + unitArr[index];
        }
        if (index == 4) {
          return count.toFixed(2) + unitArr[index];
        }
      }
    }
  },
  methods: {
    upPage(type) {
      this.$emit('pageIndex', this.broad_show_item_index - 1);
      this.broad_show_item_index = this.broad_show_item_index - 1;
      this.isTriggerPageUp = false;
      this.isTriggerPageDown = false;
      // 当前页第一条时查询上一页内容
      if (this.broad_show_item_index == 0) {
        this.$emit("searchBroadList", type);
        this.typePage = type;
      }
      this.$forceUpdate();
    },
    broadDelete() {
      this.$emit("broadDelete");
    },
    broadClose() {
      this.$emit("broadClose");
    },
    downFile(item) {
      let _this = this;
      let index = item.attachName.lastIndexOf("\/");
      this.$set(item, "messageId", this.$func.uuid());
      // 新增广播文件下载标识字段flag:'broad'
      let broadItem = Object.assign(item, {flag:'broad'});
      this.$emit("downloadListAddProcess", broadItem);
      let str = item.attachName.substring(index + 1, item.attachName.length);
      let fileName = str.lastIndexOf("."); //取到文件名开始到最后一个点的长度
      let fileNameLength = str.length; //取到文件名长度
      let fileFormat = str.substring(fileName + 1, fileNameLength); //截
      console.log(item,'文件信息');
      let info = _this.$shell("SaveFileUrl", [
        _this.myInfo.id,
        _this.$store.state.url +
          "api/v1/oim/file/download.do?id=" +
          item.attach,
        item.messageId,
        str,
        fileFormat,
        ""
      ]);
      info = JSON.parse(info);
      console.log(info, "下载文件");
      if (info.return_code == "TRUE") {
        this.$Message.success("文件下载中,请稍后");
        this.$set(item, "loading", true);
      };
    }
  },
  components: {},
  created() {}
};
</script>
<style scoped>
.broad_list_detail{
  margin: 0;
  padding: 0;
  width: 100%;
  position: relative;
}
.broad_list_first{
  height: 37px;
}
.broad_list_detail .broad_file_wrap{
  margin-bottom: 10px;
}
</style>
